package com.ruoyi.funding.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;

import com.ruoyi.funding.domain.FundingAllocation;
import com.ruoyi.funding.domain.FundingAllocationCopy;
import com.ruoyi.funding.service.IFundingAllocationCopyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.funding.mapper.FundingAllocationMapper;

import com.ruoyi.funding.service.IFundingAllocationService;
import org.springframework.transaction.annotation.Transactional;

import static com.ruoyi.common.security.utils.SecurityUtils.getUsername;

/**
 * 资金拨款记录Service业务层处理
 * 
 * @author ycl
 * @date 2025-01-06
 */
@Service
public class FundingAllocationServiceImpl implements IFundingAllocationService 
{
    @Autowired
    private FundingAllocationMapper fundingAllocationMapper;

    @Autowired
    private IFundingAllocationCopyService fundingAllocationCopyService;



    /**
     * 查询资金拨款记录
     * 
     * @param allocationId 资金拨款记录主键
     * @return 资金拨款记录
     */
    @Override
    public FundingAllocation selectFundingAllocationByAllocationId(Long allocationId)
    {
        return fundingAllocationMapper.selectFundingAllocationByAllocationId(allocationId);
    }

    /**
     * 查询资金拨款记录列表
     * 
     * @param fundingAllocation 资金拨款记录
     * @return 资金拨款记录
     */
    @Override
    public List<FundingAllocation> selectFundingAllocationList(FundingAllocation fundingAllocation)
    {
        return fundingAllocationMapper.selectFundingAllocationList(fundingAllocation);
    }

    /**
     * 新增资金拨款记录
     * 
     * @param fundingAllocation 资金拨款记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertFundingAllocation(FundingAllocation fundingAllocation)
    {
        fundingAllocation.setCreateTime(DateUtils.getNowDate());
        fundingAllocation.setCreateBy(getUsername());
        // 溯源回溯
        FundingAllocationCopy fundingAllocationCopy = new FundingAllocationCopy();
        // 插入 FundingAllocation 到数据库
        int count=fundingAllocationMapper.insertFundingAllocation(fundingAllocation);
        // 修改为从 fundingAllocation 复制属性到 fundingAllocationCopy
        BeanUtils.copyProperties(fundingAllocation, fundingAllocationCopy);

        fundingAllocationCopy.setHistoryType("增加");

        // 保存 FundingAllocationCopy 到数据库
        fundingAllocationCopyService.insertFundingAllocationCopy(fundingAllocationCopy);

        return count;
    }

    /**
     * 修改资金拨款记录
     * 
     * @param fundingAllocation 资金拨款记录
     * @return 结果
     */
    @Override
    @Transactional
    public int updateFundingAllocation(FundingAllocation fundingAllocation)
    {
        // 获取当前用户,时间
        fundingAllocation.setUpdateTime(new Date());
        fundingAllocation.setUpdateBy(getUsername());

        // 溯源回溯：创建副本并保存修改历史
        FundingAllocationCopy fundingAllocationCopy = new FundingAllocationCopy();
        BeanUtils.copyProperties(fundingAllocation, fundingAllocationCopy);
        fundingAllocationCopy.setHistoryType("修改");
        fundingAllocationCopyService.insertFundingAllocationCopy(fundingAllocationCopy);

        // 更新 FundingAllocation 到数据库
        return fundingAllocationMapper.updateFundingAllocation(fundingAllocation);
    }

    /**
     * 批量删除资金拨款记录
     * 
     * @param allocationIds 需要删除的资金拨款记录主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteFundingAllocationByAllocationIds(Long[] allocationIds)
    {

        for (Long allocationId : allocationIds) {
            // 查询要删除的资金拨款记录
            FundingAllocation fundingAllocation = fundingAllocationMapper.selectFundingAllocationByAllocationId(allocationId);
            if (fundingAllocation != null) {
                //记录删除时间
                fundingAllocation.setDeleteTime(new Date());
                fundingAllocation.setDeleteBy(getUsername());
                fundingAllocationMapper.updateFundingAllocation(fundingAllocation);
                // 创建副本并保存删除历史
                FundingAllocationCopy fundingAllocationCopy = new FundingAllocationCopy();
                BeanUtils.copyProperties(fundingAllocation, fundingAllocationCopy);
                fundingAllocationCopy.setHistoryType("删除");
                fundingAllocationCopyService.insertFundingAllocationCopy(fundingAllocationCopy);
            }
        }

        // 执行删除操作

        return fundingAllocationMapper.deleteFundingAllocationByAllocationIds(allocationIds);
    }

    /**
     * 删除资金拨款记录信息
     * 
     * @param allocationId 资金拨款记录主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteFundingAllocationByAllocationId(Long allocationId)
    {
        FundingAllocation fundingAllocation = fundingAllocationMapper.selectFundingAllocationByAllocationId(allocationId);
        //记录删除时间
        fundingAllocation.setDeleteTime(new Date());
        fundingAllocation.setDeleteBy(getUsername());
        fundingAllocationMapper.updateFundingAllocation(fundingAllocation);
        // 创建副本并保存删除历史
        FundingAllocationCopy fundingAllocationCopy = new FundingAllocationCopy();
        BeanUtils.copyProperties(fundingAllocation, fundingAllocationCopy);
        fundingAllocationCopy.setHistoryType("删除");
        fundingAllocationCopyService.insertFundingAllocationCopy(fundingAllocationCopy);

        return fundingAllocationMapper.deleteFundingAllocationByAllocationId(allocationId);
    }

    /*回溯更新*/
    @Override
    @Transactional
    public int updateFundingAllocation1(FundingAllocation fundingAllocation) {
        // 获取当前用户,时间
        fundingAllocation.setUpdateTime(new Date());
        fundingAllocation.setUpdateBy(getUsername());

        // 溯源回溯：创建副本并保存修改历史
        FundingAllocationCopy fundingAllocationCopy = new FundingAllocationCopy();
        BeanUtils.copyProperties(fundingAllocation, fundingAllocationCopy);
        fundingAllocationCopy.setHistoryType("还原");
        fundingAllocationCopyService.insertFundingAllocationCopy(fundingAllocationCopy);

        // 更新 FundingAllocation 到数据库
        return fundingAllocationMapper.updateFundingAllocation1(fundingAllocation);
    }
}
